package com.enqualcomm.kids.mvp.reai;

import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.bean.TerminalIcon;
import com.enqualcomm.kids.config.ImageUriFactory;
import com.enqualcomm.kids.view.fencing.IFencingPanelView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yinzhiqun on 2016/9/24.
 */

public class HeaderAdapter extends PagerAdapter {
    //    public static final int MAX_ELEVATION_FACTOR = 8;
//    public static final int BASE_ELEVATION = 3;
    private List<View> mViews;
    private List<TerminalIcon> icons;

    public HeaderAdapter(List<TerminalIcon> icons) {
        this.icons = icons;
        this.mViews = new ArrayList<>();

        if (icons != null) {
            for (int i = 0; i < icons.size(); i++) {
                mViews.add(null);
            }
        }
    }

    public void setData(List<TerminalIcon> icons) {
        this.icons = icons;
        this.mViews = new ArrayList<>();

        if (icons != null) {
            for (int i = 0; i < icons.size(); i++) {
                mViews.add(null);
            }
        }

        notifyDataSetChanged();
    }

    public View getViewAt(int position) {

        if (position < mViews.size()) {
            return mViews.get(position);
        } else {
            return null;
        }
    }

    @Override
    public int getCount() {
        return icons == null ? 0 : icons.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    private boolean b = true;

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext())
                .inflate(R.layout.terminal_header, container, false);

        SimpleDraweeView header_iv = (SimpleDraweeView) view.findViewById(R.id.header_iv);
        TextView name_tv = (TextView) view.findViewById(R.id.name_tv);
        final TerminalIcon terminalIcon = icons.get(position);
        header_iv.setImageURI(ImageUriFactory.getFileUri(terminalIcon.imagePath));
        name_tv.setText(terminalIcon.terminalName);

        if (b) {
            b = false;
//            header_iv.setBackgroundResource(R.drawable.header_n1);
            view.setScaleX(1.4f);
            view.setScaleY(1.4f);
        }

        Listener listener = new Listener(position);
        view.setOnClickListener(listener);
        view.setOnLongClickListener(listener);

        view.setTag(header_iv);
        mViews.set(position, view);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        if (icons != null && icons.size() == 0) {
            return;
        }
        mViews.set(position, null);
    }

    class Listener implements View.OnClickListener, View.OnLongClickListener {
        int position;

        public Listener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            onItemClick(position);
        }

        @Override
        public boolean onLongClick(View v) {
            onItemLongClick(position);
            return true;
        }
    }

    public void onItemClick(int position) {
        if (itemClickListener != null) {
            itemClickListener.onItemClick(position);
        }
    }

    public void onItemLongClick(int position) {
        if (itemClickListener != null) {
            itemClickListener.onItemLongClick(position);
        }
    }

    private ItemClickListener itemClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(int position);

        void onItemLongClick(int position);
    }
}
